package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.core.entity.jz.AgDonateDetail;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.mapper.jz.AgDonateDetailMapper;
import com.sxbbc.common.core.service.IAgDonateDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代理贡献值明细 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-07-10
 */
@Service
public class AgDonateDetailServiceImpl extends ServiceImpl<AgDonateDetailMapper, AgDonateDetail> implements IAgDonateDetailService {

    @Override
    public IPage<AgDonateDetail> queryAgentDonateDetail(BasePage<AgDonateDetail> page, AgDonateDetail donate) {
        QueryWrapper<AgDonateDetail> wrapper = queryWrapper(donate);
        wrapper.orderByDesc("id");
        if (page.isPaging()) {
            super.page(page,wrapper);
        }else{
            List<AgDonateDetail> list = super.list(wrapper);
            page.setRecords(list);
        }
        for (AgDonateDetail d : page.getRecords()) {
            d.setTypeName(HandleTypeEnums.getName(d.getTypeId()));
        }
        return page;
    }

    private QueryWrapper<AgDonateDetail> queryWrapper(AgDonateDetail donate) {
        QueryWrapper<AgDonateDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("agid",donate.getAgid());
        if (StringUtils.isNotBlank(donate.getQueryTime())) {
            String[] times = donate.getQueryTime().split("~");
            wrapper.gt("create_time",times[0].trim());
            wrapper.lt("create_time",times[1].trim());
        }
        if (StringUtils.isNotBlank(donate.getPayCode())) {
            wrapper.eq("pay_code",donate.getPayCode());
        }
        if (donate.getTypeId() != null) {
            wrapper.eq("type_id",donate.getTypeId());
        }
        return wrapper;
    }

    @Override
    public boolean updateByAgid(AgDonateDetail donateDetail) {
        QueryWrapper<AgDonateDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("agid",donateDetail.getAgid());
        return super.update(donateDetail,wrapper);
    }
}
